package com.platform.web.lisenter;

import com.alibaba.fastjson.JSONObject;
import com.platform.base.domain.BaseLocation;
import com.platform.base.domain.MeterAnalysis;
import com.platform.base.domain.MeterInfo;
import com.platform.base.service.IBaseLocationService;
import com.platform.base.service.IMeterAnalysisService;
import com.platform.base.service.IMeterInfoService;
import com.platform.common.manager.AsyncManager;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.StringUtils;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.common.utils.spring.SpringUtils;
import com.platform.data.domain.MeterData;
import com.platform.data.domain.RuleAlarm;
import com.platform.data.domain.ValveData;
import com.platform.data.domain.ValveRead;
import com.platform.data.service.IMeterDataService;
import com.platform.data.service.IRuleAlarmService;
import com.platform.data.service.IValveReadService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;

@Component
@Slf4j
public class MeterListener {

    @Autowired
    private RedisUtil redisUtil;

    @RabbitListener(queues = "meter_queue")
    @RabbitHandler
    public void receiveMeter(JSONObject object, Channel channel, Message message) {
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            String messageId = message.getMessageProperties().getHeaders().get("spring_returned_message_correlation").toString();
            if (redisUtil.hasKey("message:" + messageId)) {
                return;
            }
            redisUtil.set("message:" + messageId, messageId, 1000);
            //执行异步保存数据
            log.info("执行异步保存热表设备数据：" + object.getString("meterDatas"));
            AsyncManager.me().execute(MeterPoolTask.saveMeter(object.getString("meterDatas"), object.getDate("createTime")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/**
 * 异步任务保存热表数据
 */
@Slf4j
class MeterPoolTask {

    private static IMeterDataService meterDataService;
    private static IMeterInfoService meterInfoService;
    private static IBaseLocationService baseLocationService;
    private static IValveReadService valveReadService;
    private static IMeterAnalysisService meterAnalysisService;


    public static TimerTask saveMeter(final String meterDatas, final Date createTime) {

        meterDataService = SpringUtils.getBean(IMeterDataService.class);

        meterInfoService = SpringUtils.getBean(IMeterInfoService.class);

        baseLocationService = SpringUtils.getBean(IBaseLocationService.class);

        valveReadService = SpringUtils.getBean(IValveReadService.class);
        meterAnalysisService = SpringUtils.getBean(IMeterAnalysisService.class);


        return new TimerTask() {
            @Override
            public void run() {
                String[] msgs = meterDatas.split(" ");
                int datalength = NettyUtil.get10HexNum(msgs[9] + msgs[10]);
                String meterId = msgs[3] + msgs[4] + msgs[5] + msgs[6];
                String  datas = NettyUtil.strChange(msgs, 11, datalength);
                String[] contents = datas.split(" ");
                MeterInfo info = meterInfoService.selectMeterInfoById(meterId);
                long typeId= info.getTypeId();
                saveMeter05(contents, createTime,typeId);
             /*   int datalength = contents.length;
                if (datalength == 232) {
                    saveMeter05(contents, createTime,1L);
                } else if (datalength == 185) {
                    saveMeter02(contents, createTime);
                } else if (datalength == 82) {
                    saveMeter03(contents, createTime);
                } else if (datalength == 63) {
                    saveMeter04(contents, createTime);
                } else{
                    saveMeter05(contents, createTime, 1L);
                }*/
            }
        };
    }

    /**
     * 将兰吉尔获取的数据保存到数据库
     *
     * @param contents
     */
    private static void saveMeter01(String[] contents, Date createTime) {
        DecimalFormat format = new DecimalFormat("0.000");

        //热表id
        String meterId = contents[10] + contents[9] + contents[8] + contents[7];
        MeterInfo info = meterInfoService.selectMeterInfoById(meterId);

        BaseLocation baseLocation = baseLocationService.selectBaseLocationById(String.valueOf(info.getLocationId()));
        ValveRead valveRead = valveReadService.selectValveCheckList(String.valueOf(info.getLocationId()));
        IRuleAlarmService ruleAlarmService = SpringUtils.getBean(IRuleAlarmService.class);
        RuleAlarm ruleAlarm = ruleAlarmService.selectalarmlimits("1");
        RuleAlarm ruleAlarma = ruleAlarmService.selectalarmlimits("2");
        RuleAlarm ruleAlarmb = ruleAlarmService.selectalarmlimits("3");
        RuleAlarm ruleAlarmc = ruleAlarmService.selectalarmlimits("4");
        RuleAlarm ruleAlarmd = ruleAlarmService.selectalarmlimits("5");
        RuleAlarm ruleAlarme = ruleAlarmService.selectalarmlimits("6");
        RuleAlarm ruleAlarmf = ruleAlarmService.selectalarmlimits("7");
        RuleAlarm ruleAlarmg = ruleAlarmService.selectalarmlimits("8");
        RuleAlarm ruleAlarmh = ruleAlarmService.selectalarmlimits("21");

        try {
            //热量
            String rl = Integer.parseInt(contents[30] + contents[29] + contents[28] + contents[27]) + "";
            //流量
            String ll = contents[36] + contents[35] + contents[34] + contents[33];
            //流量单位
            String lldw = contents[32];
            if (lldw.equals("14")) {
                ll = format.format(Float.parseFloat(ll) * 0.01);
            } else if (lldw.equals("15")) {
                ll = format.format(Float.parseFloat(ll) * 0.1);
            }
            //功率
            String gl = "0";
            //功率单位
            String gldw = contents[38];
            if (!contents[41].equals("F0")) {
                gl = contents[41] + contents[40] + contents[39];
                if (gldw.equals("2D")) {
                    gl = format.format(Float.parseFloat(gl) * 0.1);
                }
            }
            //瞬时流量
            String ssll = "0";
            if (!contents[46].equals("F0")) {
                ssll = contents[46] + contents[45] + contents[44];
                ssll = format.format(Float.parseFloat(ssll) * 0.001);
            }
            //进水温度
            String jswd = format.format(Float.parseFloat(contents[50] + contents[49]) * 0.1);
            //回水温度
            String hswd = format.format(Float.parseFloat(contents[55] + contents[54]) * 0.1);
            //工作时间
            String gzsj = Integer.parseInt(contents[83] + contents[82] + contents[81] + contents[80]) + "";
            //运行时间
            String yxsj = contents[223] + contents[222] + contents[221] + contents[220];
            //将16进制数据转为二进制数组
            String str = NettyUtil.hexStringBinStr(yxsj);
            String[] strs = str.split(" ");
            //计算分钟
            String mm = NettyUtil.get10Num(strs[26] + strs[27] + strs[28] + strs[29] + strs[30] + strs[31]).toString();
            if (mm.length() < 2) {
                mm = "0" + mm;
            }
            //计算小时
            String HH = NettyUtil.get10Num(strs[19] + strs[20] + strs[21] + strs[22] + strs[23]).toString();
            if (HH.length() < 2) {
                HH = "0" + HH;
            }
            //计算日期
            String MM = NettyUtil.get10Num(strs[4] + strs[5] + strs[6] + strs[7]).toString();
            if (MM.length() < 2) {
                MM = "0" + MM;
            }
            //计算月份
            String DD = NettyUtil.get10Num(strs[11] + strs[12] + strs[13] + strs[14] + strs[15]).toString();
            if (DD.length() < 2) {
                DD = "0" + DD;
            }
            //计算年份
            String YY = "20" + NettyUtil.get10Num(strs[17] + strs[18] + strs[0] + strs[1] + strs[2] + strs[3] + strs[8] + strs[9] + strs[10]).toString();

            yxsj = YY + "-" + MM + "-" + DD + " " + HH + ":" + mm;

            MeterData meterData = new MeterData();
            meterData.setMeterId(meterId);
            meterData.setHeat(rl);
            meterData.setFlow(ll);
            meterData.setPower(gl);
            meterData.setInstant(ssll);
            meterData.setInlet(jswd);
            meterData.setOutlet(hswd);
            meterData.setWorkTime(gzsj);
            meterData.setGatherTime(yxsj);
            if (createTime == null) {
                meterData.setCreateTime(DateUtils.getNowDate());
            } else {
                meterData.setCreateTime(createTime);
            }
            meterDataService.insertMeterData(meterData);
            if ((Float.parseFloat(jswd) - Float.parseFloat(hswd)) > Float.parseFloat(ruleAlarmb.getAlarmLimits())) {

                log.error("用户" + info.getLocationId() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits())));
            }
            if (baseLocation.getStatus().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarm.getAlarmLimitsf())) {
                log.error("用户" + info.getLocationId() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf())));

            }
            if (Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmd.getAlarmLimits())) {
                log.error("用户" + info.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits())));

            }
            if (valveRead != null) {
                if (baseLocation.getStatus().equals("1") && !valveRead.getAperture().equals("0") && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarma.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (valveRead.getAperture().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmf.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) > Float.parseFloat(ruleAlarmg.getAlarmLimits()) && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarmg.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) == Float.parseFloat(ruleAlarmh.getAlarmLimits()) && Float.parseFloat(ssll) == Float.parseFloat(ruleAlarmh.getAlarmLimitsf())) {
                    log.error("缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "缴费用户,阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()), "缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }


        } catch (Exception e) {
            e.printStackTrace();

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "2", "3", "采集兰吉尔热表数据", "兰吉尔热表" + meterId + "数据有误，解析错误:" + contents));
        }
    }

    /**
     * 将恩乐曼获取的数据保存到数据库
     *
     * @param contents
     */
    private static void saveMeter02(String[] contents, Date createTime) {
        DecimalFormat format = new DecimalFormat("0.000");
        //热表id
        String meterId = contents[10] + contents[9] + contents[8] + contents[7];
        MeterInfo info = meterInfoService.selectMeterInfoById(meterId);
        BaseLocation baseLocation = baseLocationService.selectBaseLocationById(String.valueOf(info.getLocationId()));
        ValveRead valveRead = valveReadService.selectValveCheckList(String.valueOf(info.getLocationId()));
        IRuleAlarmService ruleAlarmService = SpringUtils.getBean(IRuleAlarmService.class);
        RuleAlarm ruleAlarm = ruleAlarmService.selectalarmlimits("1");
        RuleAlarm ruleAlarma = ruleAlarmService.selectalarmlimits("2");
        RuleAlarm ruleAlarmb = ruleAlarmService.selectalarmlimits("3");
        RuleAlarm ruleAlarmc = ruleAlarmService.selectalarmlimits("4");
        RuleAlarm ruleAlarmd = ruleAlarmService.selectalarmlimits("5");
        RuleAlarm ruleAlarme = ruleAlarmService.selectalarmlimits("6");
        RuleAlarm ruleAlarmf = ruleAlarmService.selectalarmlimits("7");
        RuleAlarm ruleAlarmg = ruleAlarmService.selectalarmlimits("8");
        RuleAlarm ruleAlarmh = ruleAlarmService.selectalarmlimits("21");
        try {
            //热量
            String rl = Integer.parseInt(contents[30] + contents[29] + contents[28] + contents[27]) + "";
            //流量
            String ll = contents[36] + contents[35] + contents[34] + contents[33];
            ll = format.format(Float.parseFloat(ll) * 0.01);
            //功率
            String gl = contents[42] + contents[41] + contents[40] + contents[39];
            gl = format.format(Float.parseFloat(gl) * 0.001);
            //瞬时流量
            String ssll = contents[54] + contents[53] + contents[52] + contents[51];
            ssll = format.format(Float.parseFloat(ssll) * 0.001);
            //进水温度
            String jswd = format.format(Float.parseFloat(NettyUtil.get10HexNum(contents[64]) + NettyUtil.get10HexNum(contents[63]).toString()) * 0.1);
            //回水温度
            String hswd = format.format(Float.parseFloat(NettyUtil.get10HexNum(contents[68]) + NettyUtil.get10HexNum(contents[67]).toString()) * 0.1);
            //工作时间
            String gzsj = Integer.parseInt(NettyUtil.get10HexNum(contents[76] + contents[75]).toString()) * 60 + "";
            //运行时间
            String yxsj = contents[82] + contents[81] + contents[80] + contents[79];
            //将16进制数据转为二进制数组
            String str = NettyUtil.hexStringBinStr(yxsj);
            String[] strs = str.split(" ");
            //计算分钟
            String mm = NettyUtil.get10Num(strs[26] + strs[27] + strs[28] + strs[29] + strs[30] + strs[31]).toString();
            if (mm.length() < 2) {
                mm = "0" + mm;
            }
            //计算小时
            String HH = NettyUtil.get10Num(strs[19] + strs[20] + strs[21] + strs[22] + strs[23]).toString();
            if (HH.length() < 2) {
                HH = "0" + HH;
            }
            //计算日期
            String MM = NettyUtil.get10Num(strs[4] + strs[5] + strs[6] + strs[7]).toString();
            if (MM.length() < 2) {
                MM = "0" + MM;
            }
            //计算月份
            String DD = NettyUtil.get10Num(strs[11] + strs[12] + strs[13] + strs[14] + strs[15]).toString();
            if (DD.length() < 2) {
                DD = "0" + DD;
            }
            yxsj = "2021-" + MM + "-" + DD + " " + HH + ":" + mm;


            MeterData meterData = new MeterData();
            meterData.setMeterId(meterId);
            meterData.setHeat(rl);
            meterData.setFlow(ll);
            meterData.setPower(gl);
            meterData.setInstant(ssll);
            meterData.setInlet(jswd);
            meterData.setOutlet(hswd);
            meterData.setWorkTime(gzsj);
            meterData.setGatherTime(yxsj);
            if (createTime == null) {
                meterData.setCreateTime(DateUtils.getNowDate());
            } else {
                meterData.setCreateTime(createTime);
            }
            meterDataService.insertMeterData(meterData);
            if ((Float.parseFloat(jswd) - Float.parseFloat(hswd)) > Float.parseFloat(ruleAlarmb.getAlarmLimits())) {

                log.error("用户" + info.getLocationId() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits())));
            }
            if (baseLocation.getStatus().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarm.getAlarmLimitsf())) {
                log.error("用户" + info.getLocationId() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf())));

            }
            if (Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmd.getAlarmLimits())) {
                log.error("用户" + info.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits())));

            }
            if (valveRead != null) {
                if (baseLocation.getStatus().equals("1") && !valveRead.getAperture().equals("0") && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarma.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (valveRead.getAperture().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmf.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) > Float.parseFloat(ruleAlarmg.getAlarmLimits()) && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarmg.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) == Float.parseFloat(ruleAlarmh.getAlarmLimits()) && Float.parseFloat(ssll) == Float.parseFloat(ruleAlarmh.getAlarmLimitsf())) {
                    log.error("缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "缴费用户,阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()), "缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "2", "3", "采集恩乐曼热表数据", "恩乐曼热表" + meterId + "数据有误，解析错误" + e.getMessage()));
        }
    }

    /**
     * 将代傲获取的数据保存到数据库
     *
     * @param contents
     */
    private static void saveMeter03(String[] contents, Date createTime) {
        DecimalFormat format = new DecimalFormat("0.000");
        //热表id
        String meterId = contents[10] + contents[9] + contents[8] + contents[7];
        MeterInfo info = meterInfoService.selectMeterInfoById(meterId);
        BaseLocation baseLocation = baseLocationService.selectBaseLocationById(String.valueOf(info.getLocationId()));
        ValveRead valveRead = valveReadService.selectValveCheckList(String.valueOf(info.getLocationId()));
        IRuleAlarmService ruleAlarmService = SpringUtils.getBean(IRuleAlarmService.class);
        RuleAlarm ruleAlarm = ruleAlarmService.selectalarmlimits("1");
        RuleAlarm ruleAlarma = ruleAlarmService.selectalarmlimits("2");
        RuleAlarm ruleAlarmb = ruleAlarmService.selectalarmlimits("3");
        RuleAlarm ruleAlarmc = ruleAlarmService.selectalarmlimits("4");
        RuleAlarm ruleAlarmd = ruleAlarmService.selectalarmlimits("5");
        RuleAlarm ruleAlarme = ruleAlarmService.selectalarmlimits("6");
        RuleAlarm ruleAlarmf = ruleAlarmService.selectalarmlimits("7");
        RuleAlarm ruleAlarmg = ruleAlarmService.selectalarmlimits("8");
        RuleAlarm ruleAlarmh = ruleAlarmService.selectalarmlimits("21");

        try {
            //热量
            String rl = Integer.parseInt(contents[24] + contents[23] + contents[22] + contents[21]) + "";
            //流量
            String ll = contents[30] + contents[29] + contents[28] + contents[27];
            //流量单位
            String lldw = contents[26];
            if (lldw.equals("13")) {
                ll = format.format(Float.parseFloat(ll) * 0.001);
            } else if (lldw.equals("14")) {
                ll = format.format(Float.parseFloat(ll) * 0.01);
            } else if (lldw.equals("15")) {
                ll = format.format(Float.parseFloat(ll) * 0.1);
            }
            //功率
            String gl = contents[36] + contents[35] + contents[34] + contents[33];
            if (contents[35].equals("DD") && contents[36].equals("DD")) {
                gl = "0";
            } else {
                gl = format.format(Float.parseFloat(gl) * 0.001);
            }
            //瞬时流量
            String ssll = contents[41] + contents[40] + contents[39];
            if (contents[41].equals("DD")) {
                ssll = "0";
            } else {
                ssll = format.format(Float.parseFloat(ssll) * 0.001);
            }
            //进水温度
            String jswd = format.format(Float.parseFloat(contents[45] + contents[44]) * 0.1);
            //回水温度
            String hswd = format.format(Float.parseFloat(contents[49] + contents[48]) * 0.1);
            //工作时间
            String gzsj = Integer.parseInt(contents[57] + contents[56]) * 60 + "";
            //运行时间
            String yxsj = contents[63] + contents[62] + contents[61] + contents[60];
            //将16进制数据转为二进制数组
            String str = NettyUtil.hexStringBinStr(yxsj);
            String[] strs = str.split(" ");
            //计算分钟
            String mm = NettyUtil.get10Num(strs[26] + strs[27] + strs[28] + strs[29] + strs[30] + strs[31]).toString();
            if (mm.length() < 2) {
                mm = "0" + mm;
            }
            //计算小时
            String HH = NettyUtil.get10Num(strs[19] + strs[20] + strs[21] + strs[22] + strs[23]).toString();
            if (HH.length() < 2) {
                HH = "0" + HH;
            }
            //计算日期
            String MM = NettyUtil.get10Num(strs[4] + strs[5] + strs[6] + strs[7]).toString();
            if (MM.length() < 2) {
                MM = "0" + MM;
            }
            //计算月份
            String DD = NettyUtil.get10Num(strs[11] + strs[12] + strs[13] + strs[14] + strs[15]).toString();
            if (DD.length() < 2) {
                DD = "0" + DD;
            }
            //计算年份
            String YY = "20" + NettyUtil.get10Num(strs[17] + strs[18] + strs[0] + strs[1] + strs[2] + strs[3] + strs[8] + strs[9] + strs[10]).toString();

            yxsj = YY + "-" + MM + "-" + DD + " " + HH + ":" + mm;

            MeterData meterData = new MeterData();
            meterData.setMeterId(meterId);
            meterData.setHeat(rl);
            meterData.setFlow(ll);
            meterData.setPower(gl);
            meterData.setInstant(ssll);
            meterData.setInlet(jswd);
            meterData.setOutlet(hswd);
            meterData.setWorkTime(gzsj);
            meterData.setGatherTime(yxsj);
            if (createTime == null) {
                meterData.setCreateTime(DateUtils.getNowDate());
            } else {
                meterData.setCreateTime(createTime);
            }
            meterDataService.insertMeterData(meterData);
            if ((Float.parseFloat(jswd) - Float.parseFloat(hswd)) > Float.parseFloat(ruleAlarmb.getAlarmLimits())) {

                log.error("用户" + info.getLocationId() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits())));
            }
            if (baseLocation.getStatus().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarm.getAlarmLimitsf())) {
                log.error("用户" + info.getLocationId() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf())));

            }
            if (Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmd.getAlarmLimits())) {
                log.error("用户" + info.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits())));

            }
            if (valveRead != null) {
                if (baseLocation.getStatus().equals("1") && !valveRead.getAperture().equals("0") && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarma.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (valveRead.getAperture().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmf.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) > Float.parseFloat(ruleAlarmg.getAlarmLimits()) && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarmg.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) == Float.parseFloat(ruleAlarmh.getAlarmLimits()) && Float.parseFloat(ssll) == Float.parseFloat(ruleAlarmh.getAlarmLimitsf())) {
                    log.error("缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "缴费用户,阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()), "缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "2", "3", "采集代傲热表数据", "代傲热表" + meterId + "数据有误，解析错误" + e.getMessage()));
        }
    }

    /**
     * 将金水获取的数据保存到数据库
     *
     * @param contents
     */
    private static void saveMeter04(String[] contents, Date createTime) {
        DecimalFormat format = new DecimalFormat("0.000");
        DecimalFormat format1 = new DecimalFormat("0.00");
        //热表id
        String meterId = contents[9] + contents[8] + contents[7] + contents[6];
        MeterInfo info = meterInfoService.selectMeterInfoById(meterId);
        BaseLocation baseLocation = baseLocationService.selectBaseLocationById(String.valueOf(info.getLocationId()));
        ValveRead valveRead = valveReadService.selectValveCheckList(String.valueOf(info.getLocationId()));
        IRuleAlarmService ruleAlarmService = SpringUtils.getBean(IRuleAlarmService.class);
        RuleAlarm ruleAlarm = ruleAlarmService.selectalarmlimits("1");
        RuleAlarm ruleAlarma = ruleAlarmService.selectalarmlimits("2");
        RuleAlarm ruleAlarmb = ruleAlarmService.selectalarmlimits("3");
        RuleAlarm ruleAlarmc = ruleAlarmService.selectalarmlimits("4");
        RuleAlarm ruleAlarmd = ruleAlarmService.selectalarmlimits("5");
        RuleAlarm ruleAlarme = ruleAlarmService.selectalarmlimits("6");
        RuleAlarm ruleAlarmf = ruleAlarmService.selectalarmlimits("7");
        RuleAlarm ruleAlarmg = ruleAlarmService.selectalarmlimits("8");
        RuleAlarm ruleAlarmh = ruleAlarmService.selectalarmlimits("21");

        try {
            //热量
            String rl = contents[26] + contents[25] + contents[24] + contents[23];
            //热量单位
            String rldw = contents[27];
            if (rldw.equals("05")) {
                rl = format.format(Float.parseFloat(rl) * 0.01);
            } else if (rldw.equals("06")) {
                rl = format.format(Float.parseFloat(rl));
            }
            //功率
            String gl = format.format(Float.parseFloat(contents[31] + contents[30] + contents[29] + "." + contents[28]));
            //瞬时流量
            String ssll = format.format(Float.parseFloat(contents[36] + contents[35] + "." + contents[34] + contents[33]));
            //流量
            String ll = format.format(Float.parseFloat(contents[41] + contents[40] + contents[39] + "." + contents[38]));
            //进水温度
            String jswd = format1.format(Float.parseFloat(contents[44] + "." + contents[43]));
            //回水温度
            String hswd = format1.format(Float.parseFloat(contents[47] + "." + contents[46]));
            //累计工作时间
            String gzsj = Integer.parseInt(contents[51] + contents[50] + contents[49]) + "";
            //运行时间
            String yxsj = "20" + contents[57] + "-" + contents[56] + "-" + contents[55] + " " + contents[54] + ":" + contents[53] + ":" + contents[52];

            MeterData meterData = new MeterData();
            meterData.setMeterId(meterId);
            meterData.setHeat(rl);
            meterData.setFlow(ll);
            meterData.setPower(gl);
            meterData.setInstant(ssll);
            meterData.setInlet(jswd);
            meterData.setOutlet(hswd);
            meterData.setWorkTime(gzsj);
            meterData.setGatherTime(yxsj);
            if (createTime == null) {
                meterData.setCreateTime(DateUtils.getNowDate());
            } else {
                meterData.setCreateTime(createTime);
            }
            meterDataService.insertMeterData(meterData);
            if ((Float.parseFloat(jswd) - Float.parseFloat(hswd)) > Float.parseFloat(ruleAlarmb.getAlarmLimits())) {

                log.error("用户" + info.getLocationId() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits())));
            }
            if (baseLocation.getStatus().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarm.getAlarmLimitsf())) {
                log.error("用户" + info.getLocationId() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf())));

            }
            if (Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmd.getAlarmLimits())) {
                log.error("用户" + info.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits())));

            }
            if (valveRead != null) {
                if (baseLocation.getStatus().equals("1") && !valveRead.getAperture().equals("0") && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarma.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (valveRead.getAperture().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmf.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) > Float.parseFloat(ruleAlarmg.getAlarmLimits()) && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarmg.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) == Float.parseFloat(ruleAlarmh.getAlarmLimits()) && Float.parseFloat(ssll) == Float.parseFloat(ruleAlarmh.getAlarmLimitsf())) {
                    log.error("缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "缴费用户,阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()), "缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "2", "3", "采集金水热表数据", "金水热表" + meterId + "数据有误，解析错误" + e.getMessage()));
        }
    }

    //自定义获取热表的数据保存到数据库
    private static void saveMeter05(String[] contents, Date createTime, Long typeId) {
        DecimalFormat format = new DecimalFormat("0.000");

        List<MeterAnalysis> list = meterAnalysisService.selectMeterAnalysisByTypeId(typeId);
        //为了避免多次访问数据库，将所有数据存到map中
        Map<String, MeterAnalysis> map = getMap(list);

        MeterAnalysis meterID = map.get("ID");

        int meterS = Integer.parseInt(meterID.getColumnStart());  //7
        int meterO = Integer.parseInt(meterID.getColumnEnd());    //10

        //热表id
        String meterId = strJoin(contents, meterS, meterO);

        MeterInfo info = meterInfoService.selectMeterInfoById(meterId);
        BaseLocation baseLocation = baseLocationService.selectBaseLocationById(String.valueOf(info.getLocationId()));
        ValveRead valveRead = valveReadService.selectValveCheckList(String.valueOf(info.getLocationId()));
        IRuleAlarmService ruleAlarmService = SpringUtils.getBean(IRuleAlarmService.class);
        RuleAlarm ruleAlarm = ruleAlarmService.selectalarmlimits("1");
        RuleAlarm ruleAlarma = ruleAlarmService.selectalarmlimits("2");
        RuleAlarm ruleAlarmb = ruleAlarmService.selectalarmlimits("3");
        RuleAlarm ruleAlarmc = ruleAlarmService.selectalarmlimits("4");
        RuleAlarm ruleAlarmd = ruleAlarmService.selectalarmlimits("5");
        RuleAlarm ruleAlarme = ruleAlarmService.selectalarmlimits("6");
        RuleAlarm ruleAlarmf = ruleAlarmService.selectalarmlimits("7");
        RuleAlarm ruleAlarmg = ruleAlarmService.selectalarmlimits("8");
        RuleAlarm ruleAlarmh = ruleAlarmService.selectalarmlimits("21");

        try {
            //热量
            MeterAnalysis meterRL = map.get("RL");
            int rlStart = Integer.parseInt(meterRL.getColumnStart());
            int rlEnd = Integer.parseInt(meterRL.getColumnEnd());
            String rlUnit = meterRL.getColumnUnit(); //热量单位
            float rlCoefficient = Float.parseFloat(meterRL.getColumnCoefficient());  //热量系数
            String rl = strJoin(contents, rlStart, rlEnd);
            if (StringUtils.isNotBlank(rlUnit)){
                String rldw = contents[Integer.parseInt(rlUnit)];
                if ("27".equals(rlUnit)) {
                    if (rldw.equals("05")) {       //14
                        rl = format.format(Float.parseFloat(rl) * rlCoefficient);
                    } else if (rldw.equals("06")) {                     //15
                        rl = format.format(Float.parseFloat(rl) * rlCoefficient * 100);
                    }
                }
            }else{
                rl = format.format(Float.parseFloat(rl) );
            }



            //流量
            MeterAnalysis meterAnalysisLL = map.get("LL");
            int llStart = Integer.parseInt(meterAnalysisLL.getColumnStart()); //流量-起始字段
            int llEnd = Integer.parseInt(meterAnalysisLL.getColumnEnd());   //流量-结束字段
            String llUnit = meterAnalysisLL.getColumnUnit();  //流量单位
            float llCoefficient = Float.parseFloat("0.01");
            if(meterAnalysisLL.getColumnCoefficient().equals("")){
                 llCoefficient = Float.parseFloat("0.01");
            }else
                {  llCoefficient = Float.parseFloat(meterAnalysisLL.getColumnCoefficient());  //流量系数
                }

            String ll = strJoin(contents, llStart, llEnd);
            //判断是否存在流量单位
            if (StringUtils.isNotBlank(llUnit)) {
                String lldw = contents[Integer.parseInt(llUnit)];
                //流量单位=32  兰吉尔热表   流量系数前台需要配置成0.01
                if ("32".equals(llUnit)) {
                    if (lldw.equals("14")) {       //14
                        ll = format.format(Float.parseFloat(ll) * llCoefficient);
                    } else if (lldw.equals("15")) {                     //15
                        ll = format.format(Float.parseFloat(ll) * llCoefficient * 10);
                    }
                }
                //流量单位=26   代奥热表   流量系数前台配置0.001
                if ("26".equals(llUnit)) {
                    if (lldw.equals("13")) {
                        ll = format.format(Float.parseFloat(ll) * llCoefficient);
                    } else if (lldw.equals("14")) {                     //15
                        ll = format.format(Float.parseFloat(ll) * llCoefficient * 10);
                    } else {
                        ll = format.format(Float.parseFloat(ll) * llCoefficient * 100);
                    }
                }
            } else {
                ll = format.format(Float.parseFloat(ll) * llCoefficient);
            }

            //功率
            MeterAnalysis meterGL = map.get("GL");
            String columUnit = meterGL.getColumnUnit(); //功率单位
            int columnStart = Integer.parseInt(meterGL.getColumnStart()); //功率-字段开始位置  39
            int columnEnd = Integer.parseInt(meterGL.getColumnEnd()); //功率-字段结束位置    41

            double columnCoefficient = Double.parseDouble(meterGL.getColumnCoefficient()); //功率系数
            String gl = "0";
            //判断是否有功率单位的配置，如果存在则处理安吉尔热表
            if (StringUtils.isNotBlank(columUnit)) {
                String gldw = contents[Integer.parseInt(columUnit)]; //功率单位 contents[38];
                if (!contents[columnEnd].equals("F0")) {
                    gl = strJoin(contents, columnStart, columnEnd);
                    if (gldw.equals("2D")) {
                        gl = format.format(Double.parseDouble(gl) * columnCoefficient);
                    }
                }
            } else {
                String temp = strJoin(contents, columnStart, columnEnd);
                gl = format.format(Double.parseDouble(temp) * columnCoefficient);
            }

            //瞬时流量
            MeterAnalysis meterSSLL = map.get("SSLL");
            int ssllStart = Integer.parseInt(meterSSLL.getColumnStart()); //瞬时流量-开始字段
            int ssllEnd = Integer.parseInt(meterSSLL.getColumnEnd()); //瞬时流量-结束字段
            String ssllUnit = meterSSLL.getColumnUnit();    //瞬时流量单位
            Float ssllCoefficient = Float.parseFloat(meterSSLL.getColumnCoefficient()); //瞬时流量系数
            String temp = strJoin(contents, ssllStart, ssllEnd);
            String ssll = "0";
            if (StringUtils.isNotBlank(ssllUnit)) {
                int sslldw = Integer.parseInt(ssllUnit);
                if (!contents[sslldw].equals("F0")) {
                    ssll = format.format(Float.parseFloat(temp) * ssllCoefficient);
                }else{
                    ssll = "0";
                }
            } else {
                ssll = format.format(Float.parseFloat(temp) * ssllCoefficient);
            }

            //进水温度
            MeterAnalysis meterJSWD = map.get("JSWD");
            int jswdStart = Integer.parseInt(meterJSWD.getColumnStart());
            int jswdEnd = Integer.parseInt(meterJSWD.getColumnEnd());
            float jswdCoefficient = Float.parseFloat(meterJSWD.getColumnCoefficient());
            String jswdHex = meterJSWD.getColumnHex();
            String jswd = "0";
            if ((jswdHex)=="2") { //十进制  说明是恩乐曼热表（需要将十六进制转换成十进制）
                String jswd_temp_hex = strJoinHex(contents, jswdStart, jswdEnd);  //转成十进制倒序拼接
                jswd = format.format(Float.parseFloat(jswd_temp_hex) * jswdCoefficient);
            } else {
                String jswd_temp = strJoin(contents, jswdStart, jswdEnd);   //直接倒序拼接
                jswd = format.format(Float.parseFloat(jswd_temp) * jswdCoefficient);
            }

            //回水温度
            MeterAnalysis meterHSWD = map.get("HSWD");
            int hswdStart = Integer.parseInt(meterHSWD.getColumnStart());
            int hswdEnd = Integer.parseInt(meterHSWD.getColumnEnd());
            float hswdCoefficient = Float.parseFloat(meterHSWD.getColumnCoefficient());
            String hswdHex = meterJSWD.getColumnHex();
            String hswd = "0";
            if((hswdHex)=="2"){    //十进制  说明是恩乐曼热表（需要将十六进制转换成十进制）
                String hswd_temp_hex = strJoinHex(contents, hswdStart, hswdEnd);  //转成十进制倒序拼接
                hswd = format.format(Float.parseFloat(hswd_temp_hex) * hswdCoefficient);
            }else{
                String hswd_temp = strJoin(contents, jswdStart, jswdEnd);   //直接倒序拼接
                hswd = format.format(Float.parseFloat(hswd_temp) * hswdCoefficient);
            }

            //工作时长
            MeterAnalysis meterGZSC = map.get("GZSC");
            int gzscStart = Integer.parseInt(meterGZSC.getColumnStart());
            int gzscEnd = Integer.parseInt(meterGZSC.getColumnEnd());
            float gzscCoefficient = Float.parseFloat(meterGZSC.getColumnCoefficient());
            String gzscHex = meterGZSC.getColumnHex();
            String gzsc = "0";
            if("2".equals(gzscHex)){    //十进制  说明是恩乐曼热表（需要将十六进制转换成十进制）
                String gzsc_temp_hex = strJoinHex(contents, gzscStart, gzscEnd);  //转成十进制倒序拼接
                gzsc = format.format(Float.parseFloat(gzsc_temp_hex) * gzscCoefficient); //前台配置系数为60
            }else{
                String gzsc_temp = strJoin(contents, gzscStart, gzscEnd);   //直接倒序拼接
                gzsc = format.format(Float.parseFloat(gzsc_temp) * gzscCoefficient); //根据热表不同配置不同的系数
            }

            //时间单位
            MeterAnalysis meterYXSJ = map.get("YXSJ");
            int yxsjStart = Integer.parseInt(meterYXSJ.getColumnStart());   //运行时间-开始字段
            int yxsjEnd = Integer.parseInt(meterYXSJ.getColumnEnd());   //运行时间-结束字段
            String yxsjHex = meterYXSJ.getColumnHex();
            String yxsjTime = meterYXSJ.getColumnTime();
            String yxsj="";
            String[] strs=null;
            if("1".equals(yxsjTime)){  //如果是1，则需要拆分字符串
                yxsj= strJoin(contents,yxsjStart,yxsjEnd);
                String str = NettyUtil.hexStringBinStr(yxsj);
                strs = str.split(" ");

                //计算分钟
                String mm = NettyUtil.get10Num(strs[26] + strs[27] + strs[28] + strs[29] + strs[30] + strs[31]).toString();
                if (mm.length() < 2) {
                    mm = "0" + mm;
                }
                //计算小时
                String HH = NettyUtil.get10Num(strs[19] + strs[20] + strs[21] + strs[22] + strs[23]).toString();
                if (HH.length() < 2) {
                    HH = "0" + HH;
                }
                //计算日期
                String MM = NettyUtil.get10Num(strs[4] + strs[5] + strs[6] + strs[7]).toString();
                if (MM.length() < 2) {
                    MM = "0" + MM;
                }
                //计算月份
                String DD = NettyUtil.get10Num(strs[11] + strs[12] + strs[13] + strs[14] + strs[15]).toString();
                if (DD.length() < 2) {
                    DD = "0" + DD;
                }


                    String curYear =String.valueOf(DateUtils.getCurYear());  //获取当前年份yyyy
                    yxsj = curYear+"-" + MM + "-" + DD + " " + HH + ":" + mm;

            }else{  //金水热表
                //如果是0，则直接进行字符串倒序拼接，然后进行时间格式化
                String sj_temp ="20"+strJoin(contents,yxsjStart,yxsjEnd);
                Date date = DateUtils.strToDate(sj_temp,"yyyyMMddHHmmss");
                yxsj = DateUtils.dateToStr(date, "yyyy-MM-dd HH:mm:ss");
            }



            MeterData meterData = new MeterData();
            meterData.setMeterId(meterId);
            meterData.setHeat(rl);
            meterData.setFlow(ll);
            meterData.setPower(gl);
            meterData.setInstant(ssll);
            meterData.setInlet(jswd);
            meterData.setOutlet(hswd);
            meterData.setWorkTime(gzsc);
            meterData.setGatherTime(yxsj);
            if (createTime == null) {
                meterData.setCreateTime(DateUtils.getNowDate());
            } else {
                meterData.setCreateTime(createTime);
            }
            meterDataService.insertMeterData(meterData);
            if ((Float.parseFloat(jswd) - Float.parseFloat(hswd)) > Float.parseFloat(ruleAlarmb.getAlarmLimits())) {

                log.error("用户" + info.getLocationId() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "供回水温差大于" + Float.parseFloat(ruleAlarmb.getAlarmLimits())));
            }
            if (baseLocation.getStatus().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarm.getAlarmLimitsf())) {
                log.error("用户" + info.getLocationId() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "未缴费用户，流量大于" + Float.parseFloat(ruleAlarm.getAlarmLimitsf())));

            }
            if (Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmd.getAlarmLimits())) {
                log.error("用户" + info.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()));
                AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits()), "用户" + baseLocation.getLocationName() + "流量大于" + Float.parseFloat(ruleAlarmd.getAlarmLimits())));

            }
            if (valveRead != null) {
                if (baseLocation.getStatus().equals("1") && !valveRead.getAperture().equals("0") && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarma.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "1", "缴费用户，阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度不为零，流量小于" + Float.parseFloat(ruleAlarma.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (valveRead.getAperture().equals("0") && Float.parseFloat(ssll) > Float.parseFloat(ruleAlarmf.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "2", "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度为零，流量大于" + Float.parseFloat(ruleAlarmf.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) > Float.parseFloat(ruleAlarmg.getAlarmLimits()) && Float.parseFloat(ssll) < Float.parseFloat(ruleAlarmg.getAlarmLimitsf())) {
                    log.error("用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf()), "用户" + baseLocation.getLocationName() + "阀开度大于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量小于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }
            if (valveRead != null) {
                if (Float.parseFloat(valveRead.getAperture()) == Float.parseFloat(ruleAlarmh.getAlarmLimits()) && Float.parseFloat(ssll) == Float.parseFloat(ruleAlarmh.getAlarmLimitsf())) {
                    log.error("缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()));
                    AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "5", "3", "缴费用户,阀开度等于" + Float.parseFloat(ruleAlarmh.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmh.getAlarmLimitsf()), "缴费用户" + baseLocation.getLocationName() + "阀开度等于" + Float.parseFloat(ruleAlarmg.getAlarmLimits()) + "，流量等于" + Float.parseFloat(ruleAlarmg.getAlarmLimitsf())));
                }
            }


        } catch (Exception e) {
            e.printStackTrace();

            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(meterId, String.valueOf(info.getLocationId()), "2", "3", "采集兰吉尔热表数据", "兰吉尔热表" + meterId + "数据有误，解析错误:" + contents));
        }
    }


    //字符串拼接,暂时使用倒序拼接
    private static String strJoin(String[] contents, int start, int end) {
        StringBuilder sb = new StringBuilder();
        for (int i = end; i >= start; i--) {
            sb.append(contents[i]);
        }
        return sb.toString();
    }

    //十六进制转十进制，倒序拼接
    private static String strJoinHex(String[] contents, int start, int end) {
        StringBuilder sb = new StringBuilder();
        for (int i = end; i >= start; i--) {
            sb.append(NettyUtil.get10HexNum(contents[i]));
        }
        return sb.toString();
    }

    //为了避免多次访问数据库，将所有数据存到map中
    private static Map getMap(List<MeterAnalysis> list) {
        Map<String, MeterAnalysis> meterMap = new HashMap<>();
        for (MeterAnalysis meterAnalysis : list) {
            if ("ID".equals(meterAnalysis.getVariableName())) { meterMap.put("ID", meterAnalysis);}
            if ("RL".equals(meterAnalysis.getVariableName())) {meterMap.put("RL", meterAnalysis);}
            if ("LL".equals(meterAnalysis.getVariableName())) {meterMap.put("LL", meterAnalysis);}
            if ("GL".equals(meterAnalysis.getVariableName())) {
                meterMap.put("GL", meterAnalysis);
            }
            if ("SSLL".equals(meterAnalysis.getVariableName())) {
                meterMap.put("SSLL", meterAnalysis);
            }
            if ("JSWD".equals(meterAnalysis.getVariableName())) {
                meterMap.put("JSWD", meterAnalysis);
            }
            if ("HSWD".equals(meterAnalysis.getVariableName())) {
                meterMap.put("HSWD", meterAnalysis);
            }
            if ("GZSC".equals(meterAnalysis.getVariableName())) {
                meterMap.put("GZSC", meterAnalysis);
            }
            if ("YXSJ".equals(meterAnalysis.getVariableName())) {
                meterMap.put("YXSJ", meterAnalysis);
            }
        }
        return meterMap;

    }


}